package com.eightywork.life.activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

import cn.jpush.android.api.JPushInterface;

import com.eightywork.life.R;
import com.eightywork.life.activity.BaseActivity;
import com.eightywork.life.fragment.SpaceContentFragment;
import com.eightywork.life.service.CacheService;
import com.eightywork.life.service.CpService;
import com.eightywork.life.service.SpaceService;
import com.eightywork.life.util.MyHttpException;
import com.umeng.analytics.MobclickAgent;

public class SpaceActivity extends BaseActivity implements OnClickListener {
	private ImageButton leftButton;
	private FragmentManager fm;
	private FragmentTransaction tr;
	private boolean isFromReceive;
	private long count = 0;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_space);
		initView();
		initData();
	}
	
	@Override
	public void onResume() {		
		super.onResume();
		JPushInterface.onResume(this);
	    MobclickAgent.onResume(this);
	}
	
	@Override
	public void onPause() {
	    super.onPause();
		JPushInterface.onPause(this);
	    MobclickAgent.onPause(this);
	}

	public Fragment changeFragmentAnOnResume(Fragment changeFragment,
			Bundle args) {
		fm = getSupportFragmentManager();
		tr = fm.beginTransaction();
		if (args != null)
			changeFragment.setArguments(args);
		tr.replace(R.id.space_content, changeFragment);
		tr.commitAllowingStateLoss();
		return changeFragment;
	}

	private void initView() {
		leftButton = (ImageButton) findViewById(R.id.button_left);
		leftButton.setOnClickListener(this);
		String uid = getIntent().getStringExtra("uid");
		Bundle bundle = new Bundle();
		bundle.putSerializable("uid", uid);
		changeFragmentAnOnResume(new SpaceContentFragment(), bundle);
	}

	private void initData() {
		isFromReceive = getIntent().getBooleanExtra("isFromReceive", false);
	}
	
//	if (isFromReceive) {
	class Task extends AsyncTask<String, String, String> {

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			try {
				count = SpaceService.noticecount() + CpService.pmcount();
			} catch (MyHttpException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}
	     
	};
	
		
//		System.out.println("count------in------->" + count);
//	}
	

	@Override
	public void onClick(View v) {
		if (leftButton == v) {
			if (isFromReceive) {
				if (count == 0) {
				Intent intent = new Intent(context, IndexActivity.class);
				context.startActivity(intent);
				 overridePendingTransition(R.anim.push_left_in,R.anim.push_right_out);
			    } else if (count > 0) {
				   Intent intent = new Intent(context, MsgListActivity.class);				  
				   context.startActivity(intent);
				   overridePendingTransition(R.anim.push_left_in,R.anim.push_right_out);
			    } 
			} 	
			finish();
		}
	}
}
